#!/usr/bin/env node
const path = require('path');
const { program } = require('commander');
const { execSync } = require('child_process');
const fs = require('fs');
const { copyFiles } = require('./copy');

const staticPath = path.resolve(path.join(__dirname, '../static'));
const templatePath = path.resolve(path.join(__dirname, '../template'));

console.log(__dirname, staticPath, templatePath);

program
  .option('-m, --model <model-path>', '模型目录')
  .option('-o, --output <output-path>', '输出目录, 默认输出: 模板目录/out')
  .option('--app [app-code-name]', '发布模式改为应用, 模型目录将识别为应用')
  .parse(process.argv);

const { model, output, app } = program.opts();

const modelPath = path.resolve(model);
const outputPath = path.resolve(output);

console.log(modelPath, outputPath);

copyFiles(staticPath, outputPath);

fs.renameSync(
  path.join(outputPath, '.gitignore.bak'),
  path.join(outputPath, '.gitignore'),
);

const binPath = path.resolve(path.join(__dirname, '../node_modules/.bin'));

execSync(
  `${binPath}/ibizlab pub -m ${modelPath} -t ${templatePath} -o ${outputPath}/src/publish --app ${app} && ${binPath}/prettier --ignore-path '' '${outputPath}/src/publish/**/*.(ts|vue|tsx)' -w`,
);
